package com.barunsw.bwf.ems.neoarena.slot;

import java.util.ArrayList;
import java.util.List;

import org.omg.CORBA.StringHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import NAS.oper_mode_enum;
import NAS.req_cmd_st;
import NAS.res_cmd_st_seqHolder;

import com.barunsw.bwf.ems.neoarena.constants.AdminStatus;
import com.barunsw.bwf.ems.neoarena.constants.OperStatus;
import com.barunsw.bwf.ems.neoarena.model.Slot;
import com.barunsw.bwf.ems.neoarena.ne.NasService;
import com.barunsw.bwf.ems.neoarena.ne.tamms.sfh.M2000_MODE;
import com.barunsw.bwf.ems.neoarena.ne.tamms.sfh.MMC_SLOT_INT_t;
import com.barunsw.bwf.ems.neoarena.ne.tamms.sfh.SfhControl;

@Service
public class SlotServiceImpl implements SlotService {
	private static final Logger LOGGER = LoggerFactory.getLogger(SlotServiceImpl.class);
	
	@Autowired
	NasService nasService;
	
	@Override
	public List<Slot> getSlotList(long nodeId) throws Exception {
		List<Slot> slotList = null;
		
		req_cmd_st var_req_cmd_st = new req_cmd_st();
		var_req_cmd_st.cmd = "DIS-SLOT-INF;";
		var_req_cmd_st.async_key = "";
		var_req_cmd_st.user_flag = new int[4];
		var_req_cmd_st.var_oper_mode_enum = oper_mode_enum.ASYNC;
		
		res_cmd_st_seqHolder res_mms_meta_info_list = new res_cmd_st_seqHolder();
		
		StringHolder reason = new StringHolder();
		
		try {
			nasService.requestCmd(nodeId, var_req_cmd_st, res_mms_meta_info_list, reason);
			if ( res_mms_meta_info_list.value.length > 0 ) {
				char[] data = res_mms_meta_info_list.value[0].data;
				
				SfhControl sfhControl = new SfhControl(data);

				if ( res_mms_meta_info_list.value[0].result == 0 ) {
					slotList = new ArrayList<Slot>();
					
					M2000_MODE mode = sfhControl.getDisSlotSTS();
					MMC_SLOT_INT_t[] slotInfo = mode.slot;
					
					for ( int i = 0; i < slotInfo.length; i++ ) {
						int boardId 	= slotInfo[i].id;
						int adminState 	= slotInfo[i].admin;
						int status 		= slotInfo[i].sts;
						String name		= slotInfo[i].name;
						
						Slot oneSlot = new Slot();
						oneSlot.setId(boardId);
						oneSlot.setAdminStatus(AdminStatus.getStatus(adminState));
						oneSlot.setOperStatus(OperStatus.getStatus(status));
						oneSlot.setName(name);
						
						slotList.add(oneSlot);
					}
				}
			}
		}
		catch ( Exception ex ) {
			LOGGER.error(ex.getMessage(), ex);
		}
		
		return slotList;
	}

	@Override
	public Slot getSlot(long nodeId, int slotId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
